//
//  PlanRankingCell.m
//  WanCaiC8
//
//  Created by tangang on 2018/3/12.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "PlanRankingCell.h"
#import "TGAdditions.h"
#import "AdvertisingView.h"
@interface PlanRankingCell ()

@property(nonatomic,strong)AdvertisingView *advertisingView;

@end


@implementation PlanRankingCell{
    UIImageView     *imageRank;
    UIImageView     *headImage;
    UIImageView     *topImage;
    QXLable         *labRank;
    QXLable         *labFans;
    QXLable         *labName;
    QXLable         *labAccuracy;
    QXLable         *labNew;
    QXLable         *labNew1;
    QXLable         *labRankChange;
    QXLable         *labAdvertising;
//    UIImageView     *imageRankChange;
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        WS(weakSelf);
        UIView *leftView=[[UIView alloc]init];
        leftView.layer.masksToBounds=YES;
        leftView.layer.cornerRadius=4.0;
        leftView.layer.borderWidth=.7;
        leftView.layer.borderColor=DEFAULTBGCOLOR.CGColor;
        [self.contentView addSubview:leftView];
        [leftView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.contentView).with.offset(10);
            make.top.equalTo(weakSelf.contentView).with.offset(5);
//            make.bottom.equalTo(weakSelf.contentView).with.offset(0);
            make.width.mas_equalTo(@(real_size(204)));
        }];
        
        imageRank=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ranking_1"]];
        [leftView addSubview:imageRank];
        [imageRank mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(leftView);
            make.left.equalTo(leftView).with.offset(5);
            make.size.mas_equalTo(imageRank.image.size);
        }];
        
        labRank =[[QXLable alloc]init];
        labRank.textAlignment=NSTextAlignmentCenter;
        labRank.textColor=NAVCOLOR;
        labRank.layer.masksToBounds=YES;
        labRank.layer.cornerRadius=real_size(10);
        labRank.layer.borderWidth=.7;
        labRank.layer.borderColor=NAVCOLOR.CGColor;
        [leftView addSubview:labRank];
        [labRank mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(leftView);
            make.left.equalTo(leftView).with.offset(5);
            make.width.mas_equalTo(@(real_size(20)));
            make.height.mas_equalTo(@(real_size(20)));
        }];
        
        headImage=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"avatar"]];
        headImage.layer.masksToBounds=YES;
        headImage.layer.cornerRadius=real_size(25);
        [leftView addSubview:headImage];
        [headImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(imageRank.mas_right).with.offset(13);
            make.top.equalTo(leftView).with.offset(10);
            make.bottom.equalTo(leftView).with.offset(-10);
            make.size.mas_equalTo(CGSizeMake(real_size(50), real_size(50)));
        }];
        
        topImage=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ranking_front"]];
        [leftView addSubview:topImage];
        [topImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.bottom.equalTo(headImage);
            make.size.mas_equalTo(topImage.image.size);
        }];
        
        labName=[[QXLable alloc]initTitle];
        labName.text=@"茶之轨迹";
        labName.textColor=DEFAULTORANGECOLOR;
        [leftView addSubview:labName];
        [labName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(headImage.mas_right).with.offset(10);
            make.top.equalTo(headImage).with.offset(5);
            
        }];
        
        labFans=[[QXLable alloc]init];
        [leftView addSubview:labFans];
        [labFans mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(headImage.mas_right).with.offset(10);
            make.bottom.equalTo(headImage).with.offset(-5);
            
        }];
        
        UIView *rightView=[[UIView alloc]init];
        rightView.layer.masksToBounds=YES;
        rightView.layer.cornerRadius=4.0;
        rightView.layer.borderWidth=.7;
        rightView.layer.borderColor=DEFAULTBGCOLOR.CGColor;
        [self.contentView addSubview:rightView];
        [rightView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(weakSelf.contentView).with.offset(-7);
            make.top.equalTo(weakSelf.contentView).with.offset(5);
            make.bottom.equalTo(leftView);
            make.left.equalTo(leftView.mas_right).with.offset(15);
        }];
        
        labAccuracy=[[QXLable alloc]initGaryTitle];
        labAccuracy.text=@"7中6";
        [rightView addSubview:labAccuracy];
        [labAccuracy mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(rightView).with.offset(15);
            make.centerY.equalTo(labName);
            make.size.mas_equalTo(CGSizeMake(100, 40));
        }];
        
//        imageRankChange=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ranking_top"]];
//        [rightView addSubview:imageRankChange];
//        [imageRankChange mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.right.equalTo(rightView);
//            make.centerY.equalTo(labAccuracy);
//            make.size.mas_equalTo(imageRankChange.image.size);
//        }];
        
        labRankChange=[[QXLable alloc]initGaryTitle];
        labRankChange.text=@"升10";
        labRankChange.textAlignment=NSTextAlignmentCenter;
        [rightView addSubview:labRankChange];
        [labRankChange mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(rightView);
            make.centerY.equalTo(labAccuracy);
            make.size.mas_equalTo(CGSizeMake(60, 40));
        }];
        
        labNew=[[QXLable alloc] initGaryText];
        labNew.text=@"最大连中5期";
        labNew.font=[UIFont systemFontOfSize:12];
        [rightView addSubview:labNew];
        [labNew mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(rightView).with.offset(5);
            make.centerY.equalTo(labFans);
            make.height.mas_equalTo(@30);
            make.right.equalTo(rightView);
        }];
        
        labNew1=[[QXLable alloc] initGaryText];
        labNew1.text=@"上期中";
        labNew1.textAlignment=NSTextAlignmentCenter;
        labNew1.font=[UIFont systemFontOfSize:12];
        [rightView addSubview:labNew1];
        [labNew1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(rightView);
            make.centerY.equalTo(labFans);
            make.height.mas_equalTo(@30);
            make.width.mas_equalTo(@60);
        }];
        
//        labAdvertising=[[QXLable alloc]initGaryText];
//        labAdvertising.userInteractionEnabled=YES;
//        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onclickAdvertising)];
//        [labAdvertising addGestureRecognizer:tap];
//        labAdvertising.textAlignment=NSTextAlignmentCenter;
//        [self.contentView addSubview:labAdvertising];
//        [labAdvertising mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.right.equalTo(weakSelf.contentView);
//            make.top.equalTo(leftView.mas_bottom).with.offset(5);
//            make.bottom.equalTo(weakSelf.contentView);
//            make.height.mas_equalTo(@0);
//        }];
        
        self.advertisingView  = [[AdvertisingView alloc]init];
        self.advertisingView.changeHeight = ^(float height) {
            [weakSelf.advertisingView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(@(height));
            }];
        };
        [self.contentView addSubview:self.advertisingView];
        [self.advertisingView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.contentView).offset(10);
            make.right.equalTo(weakSelf.contentView).offset(-10);
            make.top.equalTo(leftView.mas_bottom).offset(5);
            make.bottom.equalTo(weakSelf.contentView);
            make.height.mas_equalTo(@0);
        }];
        self.advertisingView.hidden= YES;
    }
    return self;
}

- (void)setModel:(ExpertsModel *)model{
    _model=model;
    if (model.RowNumber<=3) {
        labRank.text=@"";
        labRank.layer.borderColor=[UIColor whiteColor].CGColor;
        topImage.hidden=NO;
        imageRank.image=[UIImage imageNamed:[NSString stringWithFormat:@"ranking_%ld",model.RowNumber]];
        labName.textColor=DEFAULTORANGECOLOR;
    }else{
        labRank.layer.borderColor=NAVCOLOR.CGColor;
        imageRank.image=nil;
        topImage.hidden=YES;
        if (model.RowNumber>=100) {
            [labRank mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(@(real_size(25)));
            }];
        }else{
            [labRank mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(@(real_size(20)));
            }];
        }
        labRank.text=[NSString stringWithFormat:@"%ld",model.RowNumber];
        labName.textColor=DEFAULTTEXTBLACKCOLOR;
    }
    [headImage sd_setImageWithURL:[NSURL URLWithString:model.Avater] placeholderImage:[UIImage imageNamed:@"avatar"]];
    labName.text=model.Name;
    labAccuracy.text=model.HitRateDesc;
    labRankChange.text=[NSString stringWithFormat:@"%.0f%%",[model.HitRate floatValue]*100];
    labNew.text=[NSString stringWithFormat:@"最大连中%@期",model.MaxWin];
    labNew1.text=@"胜率";
    NSMutableAttributedString *attStrFans=[[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"粉丝数%@",model.FansNumber]];
    [attStrFans addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(3, attStrFans.length-3)];
    labFans.attributedText=attStrFans;
}

- (void)setAdModel:(AdvertisingModel *)adModel{
    _adModel=adModel;
    if (adModel) {
        
        if ([adModel.Title containsString:@"【"]) {
          labAdvertising.attributedText = [NSAttributedString tg_imageTextWithTitle:adModel.Title];
        
        } else {
           labAdvertising.text = adModel.Title;
        }
        [labAdvertising mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@(real_size(30)));
        }];
    }else{
        labAdvertising.text=nil;
        [labAdvertising mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@0);
        }];
    }
}

-(void)setModelArr:(NSMutableArray<AdvertisingModel *> *)modelArr{
    _modelArr = modelArr;
    if (modelArr.count>0) {
        self.advertisingView.dataArr = modelArr;
        
        self.advertisingView.hidden = NO;

        self.advertisingView.changeHeight(modelArr.count*40);

    }else{
        self.advertisingView.hidden = YES;
        
        self.advertisingView.changeHeight(0);
    }
}


-(void)setModelArr:(NSMutableArray<AdvertisingModel *> *)modelArr AndRowCount:(NSInteger)rowCount{
    _modelArr = modelArr;
    if (modelArr.count>0) {
        self.advertisingView.dataArr = modelArr;
        
        self.advertisingView.hidden = NO;
        
        self.advertisingView.changeHeight(rowCount*40);
        
    }else{
        self.advertisingView.hidden = YES;
        
        self.advertisingView.changeHeight(0);
    }
}

- (void)onclickAdvertising{
    if (self.adModel.TransferUrl.length<1) {
        return;
    }
    
    if ([self.adModel.Title containsString:@"】"]) {
        
        UIPasteboard *copy = [UIPasteboard generalPasteboard];
        NSRange startRange = [self.adModel.Title rangeOfString:@"【"];
        NSRange endRange = [self.adModel.Title rangeOfString:@"】"];
        NSRange range = NSMakeRange(startRange.location + startRange.length, endRange.location - startRange.location - startRange.length);
        NSString *result = [self.adModel.Title substringWithRange:range];
        
        [copy setString:result];
        
        [SVProgressHUD showSuccessWithStatus:@"复制成功"];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
            [[UIApplication sharedApplication]openURL:[NSURL URLWithString: @"weixin://"]];
        });
        
    }else{
        
        [[UIApplication sharedApplication]openURL:[NSURL URLWithString: self.adModel.TransferUrl]];
        
    }

}

@end
